package com.strongbj.iot.devices.newlight.request.handle;

import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.strongbj.core.message.IMessageHandle;
import com.strongbj.core.util.ContextUtils;
import com.strongbj.iot.devices.newlight.entity.GetTagsReaderRelationReturnCardData;
import com.strongbj.iot.devices.newlight.entity.GetTagsReaderRelationReturnData;
import com.strongbj.iot.devices.newlight.entity.Tag;
import com.strongbj.iot.devices.newlight.message.NewLightMQMessage;
import com.strongbj.iot.devices.newlight.service.TagService;
import com.strongbj.iot.mq.producer.TopicSender;

import io.netty.channel.ChannelHandlerContext;

/**
 * 获取标签Reader关系
 * @author lei_w
 *
 */
public class GetTagsReaderRelationHandle implements IMessageHandle<NewLightMQMessage, Object> {

	private static Logger logger = LogManager.getLogger(GetTagsReaderRelationHandle.class.getName());
	
	private TagService tagService = (TagService) ContextUtils.getBean("tagService");
	
	private TopicSender topicSender = (TopicSender) ContextUtils.getBean("topicSender");
	
	@Override
	public boolean isHandle(NewLightMQMessage msg) {
		if (msg.getActioncode().equals("getTagsReaderRelation")) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Object handle(ChannelHandlerContext ctx, NewLightMQMessage msg) {
		List<Tag> list = tagService.list();
		List<GetTagsReaderRelationReturnCardData> tags = new ArrayList<GetTagsReaderRelationReturnCardData>();
		for(Tag tag : list) {
			GetTagsReaderRelationReturnCardData tr = new GetTagsReaderRelationReturnCardData();
			tr.setTag(tag.getIp());
			tr.setReaderCode(tag.getReader());
			tags.add(tr);
		}
		GetTagsReaderRelationReturnData trr = new GetTagsReaderRelationReturnData();
		trr.setTags(tags);
		NewLightMQMessage mes = new NewLightMQMessage();
		mes.setActioncode("getTagsReaderRelationReturn");
		mes.setGUID(msg.getGUID());
		mes.setPostdata(trr);
		logger.info("=========getTagsReaderRelationReturn=======" + JSON.toJSONString(mes));
		topicSender.send("wh.reader.command.response", JSON.toJSONString(mes));
		return mes;
	}

	@Override
	public void destory() {
		// TODO Auto-generated method stub
		
	}

}
